package com.gxa.jjty.phy.controller;


import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.PhyUser;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.util.*;
import com.gxa.jjty.phy.service.NewsService;
import com.gxa.jjty.phy.service.PhyUserService;
import com.gxa.jjty.phy.service.ToderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/phyToder")
@Transactional(rollbackFor = Throwable.class)
public class TorderController {

    @Autowired
    private ToderService toderService;

    @Autowired
    private PhyUserService phyUserService;

    @Autowired
    private NewsService newsService;

    /*@ApiOperation("获取可接订单列表的方法")
    @PostMapping("/waitServed")

    public Map waitServed(@RequestParam Long id) throws IllegalAccessException {
        Torder torder = new Torder();
        torder.setOrderState("待接单");
        List<Torder> select = toderService.select(torder);
        Torder torder1 = new Torder();
        torder1.setTransferPhyId("/"+id.toString()+"/");
        List<Torder> select1 = toderService.selectDescLike(torder1);
        for(int i=0;i<select1.size();i++){
            select.add(select1.get(i));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取待服务订单列表的方法")
    @PostMapping("/toBeServed")
    *//*@ApiImplicitParams({
            @ApiImplicitParam(name = "phyid",value = "陪护员id"),
            //@ApiImplicitParam(name = "orderState",value = "订单状态")
    })*//*
    public Map toBeServed(@RequestParam Long phyId){
        Torder torder = new Torder();
        torder.setPhyId(phyId);
        torder.setOrderState("陪护员已接单");
        List<Torder> allById = toderService.select(torder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(allById);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取服务中订单列表的方法")
    @PostMapping("/Servering")
   *//* @ApiImplicitParams({
            @ApiImplicitParam(name = "phyid",value = "陪护员id"),
            //@ApiImplicitParam(name = "orderState",value = "订单状态")
    })*//*
    public Map Servering(@RequestParam Long phyId){
        Torder torder = new Torder();
        torder.setPhyId(phyId);
        torder.setOrderState("服务中");
        List<Torder> select = toderService.select(torder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取已完成订单列表的方法")
    @PostMapping("/endServer")
   *//* @ApiImplicitParams({
            @ApiImplicitParam(name = "phyid",value = "陪护员id"),
            //@ApiImplicitParam(name = "orderState",value = "订单状态")
    })*//*
    public Map endServer(@RequestParam Long phyId){
        Torder torder = new Torder();
        torder.setOrderState("已完成");
        torder.setPhyId(phyId);
        List<Torder> select = toderService.select(torder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }



    @ApiOperation("获取已取消订单列表的方法")
    @PostMapping("/canceledServer")
    public Map canceledServer(@RequestParam Long phyId){
        Torder torder = new Torder();
        torder.setPhyId(phyId);
        torder.setOrderState("已取消");
        List<Torder> select = toderService.select(torder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

*/

    @ApiOperation("获取各类订单的总方法")
    @PostMapping("/selectTorder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phyId",value = "陪护员id"),
            @ApiImplicitParam(name = "orderState",value = "订单状态")
    })
    public Map selectTorder(Long phyId,Integer orderState) throws IllegalAccessException {
        List<Torder> select = null;
        if(orderState==1){
            Torder torder = new Torder();
            torder.setOrderState("等待系统分配");
            select = toderService.select(torder);
            Torder torder1 = new Torder();
            torder1.setTransferPhyId("/"+phyId.toString()+"/");
            List<Torder> select1 = toderService.selectDescLike(torder1);
            for(int i=0;i<select1.size();i++){
                select.add(select1.get(i));
            }
        }else if(orderState==2){
            Torder torder = new Torder();
            torder.setPhyId(phyId);
            torder.setOrderState("陪护员已接单");
            select = toderService.select(torder);
        }else if(orderState==3){
            Torder torder = new Torder();
            torder.setPhyId(phyId);
            torder.setOrderState("服务中");
             select = toderService.select(torder);
        }else if(orderState==4){
            Torder torder = new Torder();
            torder.setOrderState("已完成");
            torder.setPhyId(phyId);
            select = toderService.select2(torder);
        }else if(orderState==5){
            Torder torder = new Torder();
            torder.setPhyId(phyId);
            torder.setOrderState("已取消");
            select = toderService.select(torder);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(select);
        return  jsonResult.getDataMap();
    }


    @ApiOperation("根据订单号/姓名/服务 来查询的方法")
    @PostMapping("/select")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search",value = "搜索内容"),
            @ApiImplicitParam(name = "orderState",value = "订单状态"),
            @ApiImplicitParam(name = "phyId",value = "陪护员id")
    })
    public Map select( String search,Integer orderState,Long phyId) throws BusinessException {
        List<Torder> torders = toderService.selectByNameOrServer(search, orderState, phyId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(torders);
        return  jsonResult.getDataMap();
    }

    @ApiOperation("陪护员接单的方法")
    @PostMapping("/accept")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phyId",value = "陪护员id"),
            @ApiImplicitParam(name = "orderId",value = "订单id")
    })
    public Map accept(Long orderId,Long phyId) throws IllegalAccessException, BusinessException {
        Torder torder = toderService.selectById(orderId);
        if(torder.getOrderState().equals("陪护员已接单")){
            throw new BusinessException(ExceptionCode.SECKILL_FAILED);
        }
        PhyUser phyUser = phyUserService.selectById(phyId);
        phyUser.setServerState("服务中"+torder.getOrderNumber());
        torder.setOrderState("陪护员已接单");
        Map<String, Double> lngAndLat = BaiduMapUtil.getLngAndLat(torder.getOrderAddress());
        Map<String, Double> lngAndLat1 = BaiduMapUtil.getLngAndLat(phyUser.getPosition());
        double distance = LocationUtil.getDistance(lngAndLat.get("lat"), lngAndLat.get("lng"), lngAndLat1.get("lat"), lngAndLat1.get("lng"));
        torder.setDistance("当前距用户"+distance+"km");
        torder.setAcceptTime(new Date());
        torder.setPhyId(phyUser.getId());
        torder.setPhyName(phyUser.getName());
        torder.setTransgerRemark("");
        torder.setTransferPhyId("");
        torder.setTransferTime(null);
        toderService.updateById(torder);
        newsService.insert(phyId,"恭喜您，接单成功","恭喜您接单成功,当前据用户"+distance+"km");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("接单成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("陪护员取消订单的方法")
    @PostMapping("/cancel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单id"),
            @ApiImplicitParam(name = "cancelRemark",value = "取消原因")
    })
    public  Map cancel(Long orderId,String cancelRemark) throws BusinessException, IllegalAccessException {
        Torder torder = toderService.selectById(orderId);
        torder.setCancelRemark(cancelRemark);
        torder.setCancelTime(new Date());
        torder.setCancelFrom(2);
        torder.setOrderState("已取消");
        System.out.println(torder.getCancelRemark());
        if(torder.getCancelRemark()==null){
            throw new BusinessException(ExceptionCode.REARSON_NULL);
        }else {
            toderService.updateById(torder);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("提交成功");
        return  jsonResult.getDataMap();
    }

    @ApiOperation("陪护员转单的方法")
    @PostMapping("/transfer")
    @ApiImplicitParams({
            //@ApiImplicitParam(name = "list",value = "收转人id的集合",required = true,paramType = "list"),
            @ApiImplicitParam(name = "number",value = "订单编号",required = true),
            @ApiImplicitParam(name = "reason",value = "转单理由",required = true)
    })
    public  Map transfer(@RequestParam List<Integer> list, String number, String reason) throws IllegalAccessException {
        StringBuilder ids = new StringBuilder("/");
        System.out.println("l:"+list+reason+number);
        for(int i=0;i<list.size();i++){
            ids.append(list.get(i));
            ids.append("/");
        }
        Torder torder = new Torder();
        torder.setOrderNumber(number);
        List<Torder> select = toderService.select(torder);
        Torder torder1 = select.get(0);
        torder1.setTransferTime(new Date());
        torder1.setOrderState("等待系统分配");
        torder1.setTransgerRemark("转单说明:"+reason);
        torder1.setTransferPhyId(ids.toString());
        toderService.updateById(torder1);
        for(int i=0;i<list.size();i++){
            newsService.insert((long)list.get(i),"您有一个新的转单","这个陪护员临时有事，快去帮下他吧");
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("您已成功转单！");
        return  jsonResult.getDataMap();
    }

    @ApiOperation("开始服务的方法")
    @PostMapping("/satarServer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "torderId",value = "订单id",required = true),
            @ApiImplicitParam(name = "phyId",value = "陪护员id",required = true)
    })
    public Map satarServer(@RequestParam Long torderId,@RequestParam Long phyId) throws IllegalAccessException {
        Torder torder = toderService.selectById(torderId);
        PhyUser phyUser = phyUserService.selectById(phyId);
        torder.setServeTime(new Date());
        torder.setOrderState("服务中");
        phyUser.setServerState("服务中"+torder.getOrderNumber());
        toderService.updateById(torder);
        phyUserService.updateById(phyUser);
        newsService.insert(phyId,"您的订单已开始服务!","您的订单已开始服务");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("你已开始服务");
        return  jsonResult.getDataMap();
    }

    @ApiOperation("完成服务的方法")
    @PostMapping("/endServer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "torderId",value = "订单id",required = true),
            @ApiImplicitParam(name = "phyId",value = "陪护员id",required = true)
    })
    public Map endServer(@RequestParam Long torderId,@RequestParam Long phyId) throws IllegalAccessException {
        Torder torder = toderService.selectById(torderId);
        PhyUser phyUser = phyUserService.selectById(phyId);
        torder.setServfinishTime(new Date());
        torder.setOrderState("已完成");
        phyUser.setServerState("空闲");
        phyUser.setOrderSum(phyUser.getOrderSum()+1);
        phyUser.setTodayOrder(phyUser.getTodayOrder()+1);
        toderService.updateById(torder);
        phyUserService.updateById(phyUser);
        newsService.insert(phyId,"您的订单已完成","您的订单已完成，请等待客户评价");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("你已开始服务");
        return  jsonResult.getDataMap();
    }

    @ApiOperation("代发起服务的方法")
    @PostMapping("/creatServer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serverType",value = "服务类型",required = true),
            @ApiImplicitParam(name = "serverName",value = "服务用户",required = true),
            @ApiImplicitParam(name = "serverId",value = "服务用户id",required = true),
            @ApiImplicitParam(name = "serverAddress",value = "服务地址",required = true),
            @ApiImplicitParam(name = "serverTime",value = "服务时间",required = true),
            @ApiImplicitParam(name = "serverPrice",value = "服务费用",required = true)
    })
    public Map creatServer(String serverType,String serverName,Long serverId,String serverAddress,String serverTime,Double serverPrice) throws ParseException {
        Torder torder = new Torder();
        java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        Date date =  formatter.parse(serverTime);
        torder.setServeType(serverType);
        torder.setOrderUser(serverName);
        torder.setCjrId(serverId);
        torder.setOrderAddress(serverAddress);
        torder.setServeTime(date);
        torder.setPayPrice(serverPrice);
        toderService.addTorder2(torder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("创建服务成功");
        return  jsonResult.getDataMap();
    }
    @ApiOperation("订单详情接口")
    @PostMapping("/orderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单id")
    })
    public Map orderDetail(String orderId){
        Torder torder = toderService.selectById(orderId);
        Map<String, Double> lngAndLat = BaiduMapUtil.getLngAndLat(torder.getOrderAddress());
        Map<String,Object> map = new HashMap<>();
        map.put("lng",lngAndLat.get("lng"));
        map.put("lat",lngAndLat.get("lat"));
        map.put("address",torder.getOrderAddress());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);
        return  jsonResult.getDataMap();
    }

}
